//
//  LinkReaderTableViewCell.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/4.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "LinkReaderTableViewCell.h"
#import "UIImageView+WebCache.h"

#define imgWidth (kWidth-20)/3

@implementation LinkReaderTableViewCell {
    UIImageView* imgView1;
    UIImageView* imgView2;
    UIImageView* imgView3;
    
    UILabel* titleLabel;
    UILabel* contentLabel;
    UILabel* sourceLabel;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.userInteractionEnabled = YES;
        
        imgView1 = [[UIImageView alloc] init];
        [self.contentView addSubview:imgView1];

        imgView2 = [[UIImageView alloc] init];
        [self.contentView addSubview:imgView2];
        
        imgView3 = [[UIImageView alloc] init];
        [self.contentView addSubview:imgView3];
        
        titleLabel = [[UILabel alloc] init];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.numberOfLines = 0;
        [self.contentView addSubview:titleLabel];
        
        contentLabel = [[UILabel alloc] init];
        contentLabel.textColor = [UIColor grayColor];
        contentLabel.font = [UIFont systemFontOfSize:12];
        contentLabel.numberOfLines = 3;
        [self.contentView addSubview:contentLabel];
        
        sourceLabel = [[UILabel alloc] init];
        sourceLabel.textColor = [UIColor grayColor];
        sourceLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:sourceLabel];
    }
    
    return self;
}

- (void)setReaderModel:(ReaderModel *)readerModel {
    if (_readerModel != readerModel) {
        _readerModel = readerModel;
        
        [self setNeedsLayout];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    NSArray* imgExtraArray = _readerModel.extraImgArray;
    
    if (imgExtraArray.count == 2) {      //有三张图片
        //图
        [imgView1 setFrame:CGRectMake(10, 50, imgWidth*2-2, imgWidth*2)];
        
        imgView2.hidden = NO;
        [imgView2 setFrame:CGRectMake(imgView1.right+2, 50, imgWidth, imgWidth-1)];
        NSString* img2Str = imgExtraArray[0][@"imgsrc"];
        if (img2Str) [imgView2 sd_setImageWithURL:[NSURL URLWithString:img2Str]];
        
        imgView3.hidden = NO;
        [imgView3 setFrame:CGRectMake(imgView1.right+2, 50+imgWidth+2, imgWidth, imgWidth-1)];
        NSString* img3Str = imgExtraArray[1][@"imgsrc"];
        if (img3Str) [imgView3 sd_setImageWithURL:[NSURL URLWithString:img3Str]];
        
        //标题
        [titleLabel setFrame:CGRectMake(10, 10, kWidth-30, 40)];
        
        //内容
        contentLabel.hidden = YES;
        
        //来源
        [sourceLabel setFrame:CGRectMake(10, imgView3.bottom+5, 200, 15)];
        
    } else {            //只有一张图片
        //图
        [imgView1 setFrame:CGRectMake(10, 5, imgWidth, self.bounds.size.height-10)];
        imgView2.hidden = YES;
        imgView3.hidden = YES;
        
        //标题
        [titleLabel setFrame:CGRectMake(20+imgWidth, 10, kWidth-imgWidth-30, 40)];
        
        //内容
        contentLabel.hidden = NO;
        NSString *contentStr = _readerModel.content;
        if (contentStr.length) {
            NSAttributedString* attrStr = [Tools attributedStrFromStr:_readerModel.content lineSpace:3];
            [contentLabel setFrame:CGRectMake(20+imgWidth, 55, kWidth-imgWidth-30, self.bounds.size.height-55-15-10)];
            contentLabel.attributedText = attrStr;
            [contentLabel sizeToFit];
        }
        
        //来源
        [sourceLabel setFrame:CGRectMake(20+imgWidth, self.bounds.size.height-25, 200, 15)];
    }
    
    
    //公共的信息
    //图片第一张
    if (_readerModel.imgUrl.length) [imgView1 sd_setImageWithURL:[NSURL URLWithString:_readerModel.imgUrl]];
    //标题内容
    NSString *titleStr = _readerModel.title;
    if (titleStr.length) {
        NSAttributedString* attrStr = [Tools attributedStrFromStr:_readerModel.title lineSpace:3];
        titleLabel.attributedText = attrStr;
        [titleLabel sizeToFit];
    }
    
    //来源
    sourceLabel.text = _readerModel.source;
}

@end
